package com.duomizhibo.phonelive.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.duomizhibo.phonelive.R;
import com.duomizhibo.phonelive.base.AbsFragment;
import com.duomizhibo.phonelive.ui.customviews.ViewPagerIndicator;
import com.duomizhibo.phonelive.utils.UIHelper;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cxf on 2017/9/5.
 */

public class PaiHangBangFragment extends AbsFragment implements View.OnClickListener {

    private Context mContext;
    private View mRootView;
    private ViewPagerIndicator mIndicator;
    private ViewPager mViewPager;
    private List<Fragment> mList;
    ZhouBangFragment zhouBangFragment;

    YueBangFragment yueBangFragment;

    FragmentTransaction transaction;

    private FragmentManager mFragmentManager;

    SlidingTabLayout tl4;

    MyPagerAdapter myPagerAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getActivity();
        mRootView = inflater.inflate(R.layout.fragment_paihang, container, false);
        list_title.add("is");
        list_title.add("and");
        tl4= (SlidingTabLayout) mRootView.findViewById(R.id.tl_4);
        mViewPager = (ViewPager) mRootView.findViewById(R.id.viewPager);
        mFragments.add(SimpleCardFragment2.getInstance(list_title.get(0)));
        myPagerAdapter=new MyPagerAdapter(getFragmentManager());
        mViewPager.setAdapter(myPagerAdapter);
        tl4.setViewPager(mViewPager);
        return mRootView;
    }
    private List<String> list_title = new ArrayList<>();
    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return list_title.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(savedInstanceState);
    }


    PagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private void initView(Bundle savedInstanceState) {
        mIndicator = (ViewPagerIndicator) mRootView.findViewById(R.id.indicator);
        mIndicator.setTitles(new String[]{"收入榜", "消费榜"});
        mIndicator.setVisibleChildCount(2);
        mFragmentManager = getActivity().getSupportFragmentManager();
        if (savedInstanceState == null) {
            mViewPager = (ViewPager) mRootView.findViewById(R.id.viewPager);
            mList = new ArrayList<>();
            zhouBangFragment=new ZhouBangFragment();
            yueBangFragment=new YueBangFragment();
            mList.add(zhouBangFragment);
            mList.add(yueBangFragment);
            FragmentManager fragmentManager=getChildFragmentManager();

            pagerAdapter=new PagerAdapter(fragmentManager);
//            mViewPager.setAdapter(pagerAdapter);
// /    mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
//                @Override
//                public Fragment getItem(int position) {
//                    return mList.get(position);
//                }
//
//                @Override
//                public int getCount() {
//                    return mList.size();
//                }
//            });
//            mViewPager.setOffscreenPageLimit(2);
//            mIndicator.setViewPager(mViewPager);
            mIndicator.setListener(new ViewPagerIndicator.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    publishPosition(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
        mRootView.findViewById(R.id.btn_search).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_search:
                UIHelper.showScreen(getActivity());
                break;
        }
    }



    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        getView().setVisibility(isVisibleToUser ? View.VISIBLE : View.GONE);
    }


    private void publishPosition(int position) {
//        for (PageChangedListener listener : mPageChangedListeners) {
//            listener.onPageChanged(position);
//        }
    }
       /*   mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }
    });*/


       private class  PagerAdapter extends FragmentPagerAdapter{

           public PagerAdapter(FragmentManager fm) {
               super(fm);
           }

           @Override
           public Fragment getItem(int position) {
               return mList.get(position);
           }

           @Override
           public int getCount() {
               return mList.size();
           }
       }


}
